package com.cxd.batch.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2015年12月24日 下午2:14:57
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
public class YeePayUtil {
	/**
	 * 查询易宝用户信息
	 * @author wtj
	 * @date 2015年12月24日下午1:38:11
	 * @param platformUserNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> queryYeePayUserInfo(String platformUserNo){
		Map<String,Object> reMap = new HashMap<String,Object>();
		HttpClient client = new HttpClient();
		// 创建一个post方法
		PostMethod postMethod = new PostMethod(YeePayConstants.RequestUrl.RequestDirectUrl);
		postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
		StringBuffer content = new StringBuffer();
		content.append("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>");
		content.append("<request platformNo='"+ YeePayConstants.Config.MER_CODE + "'>");
		content.append("<platformUserNo>" + platformUserNo+ "</platformUserNo>");
		content.append("</request>");
		// 生成密文
		String sign = CFCASignUtil.sign(content.toString());

		postMethod.setParameter("req", content.toString());
		postMethod.setParameter("sign", sign);
		postMethod.setParameter("service", "ACCOUNT_INFO");

		// 执行post方法
		try {
			client.executeMethod(postMethod);
			byte[] responseBody = postMethod.getResponseBody();
			// 响应信息
			Map<String, String> resultMap = Dom4jUtil.xmltoMap(new String(responseBody, "UTF-8"));
			String code = resultMap.get("code");
			if (code.equals("1")) {
				
				String platformNo = resultMap.get("platformNo");
				String description = resultMap.get("description");
				// 【见会员类型】
				String memberType = resultMap.get("memberType");
				// 【见会员激活状态】
				String activeStatus = resultMap.get("activeStatus");
				// 账户余额
				String balance = resultMap.get("balance");
				// 可用余额
				String availableAmount = resultMap.get("availableAmount");
				// 冻结金额
				String freezeAmount = resultMap.get("freezeAmount");
				// 绑定的卡号,没有则表示没有绑卡
				String cardNo = resultMap.get("cardNo");
				String cardStatus = resultMap.get("cardStatus");
				String bank = resultMap.get("bank");
				// 是否已授权自动投标,true 或则false
				String autoTender = resultMap.get("autoTender");
				// 表示用户是否已开通快捷支付。NORMAL 表示未升级，UPGRADE 表示已升级
				String paySwift = resultMap.get("paySwift");
				String bindMobileNo = resultMap.get("bindMobileNo");
				reMap.put("platformNo", platformNo);
				reMap.put("code", code);
				reMap.put("description", description);
				reMap.put("memberType", memberType);
				reMap.put("activeStatus", activeStatus);
				reMap.put("balance", balance);
				reMap.put("availableAmount", availableAmount);
				reMap.put("freezeAmount", freezeAmount);
				reMap.put("cardNo", cardNo);
				reMap.put("cardStatus", cardStatus);
				reMap.put("bank", bank);
				reMap.put("autoTender", autoTender);
				reMap.put("paySwift", paySwift);
				reMap.put("bindMobileNo", bindMobileNo);
			} 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
		}
		return reMap;
	}
}
